package com.apo1o.cmscore.controller;


import com.apo1o.cmscore.bean.ext.UserExt;
import com.apo1o.cmscore.dao.UserDao;
import com.apo1o.cmscore.util.MD5Util;
import com.apo1o.cmscore.util.Result;
import com.apo1o.cmscore.util.ResultCode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import com.apo1o.cmscore.service.IUserService;
import com.apo1o.cmscore.bean.User;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Apo1o
 * @since 2023-11-16
 */
@Slf4j
@Api(tags = "用户模块")
@RestController
@RequiredArgsConstructor
@RequestMapping("/auth/user")
public class UserController {
    private final IUserService userService;
    private final UserDao userDao;


    //admin使用：用户登录后首页展示个人信息需要使用
    @ApiOperation("获取用户个人信息")
    @GetMapping("/info")  //@RequestAttribute 请参考JwtInterceptor中 38行代码
    public Result getInfo(@RequestAttribute("userId") Long id){
        log.info("id:{}",id);
        System.out.println("id = " + id);
        return Result.success(userDao.selectById(id));
    }

    @ApiOperation("添加或修改")
    @PostMapping("/save")
    public Result saveOrUpdate(@RequestBody User user) {
        user.setPassword(MD5Util.MD5(user.getPassword()));
    userService.saveOrUpdate(user);
        return Result.success("新增成功");
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id) {
    userService.removeById(id);
        return Result.success();
    }

    @ApiOperation("批量删除")
    @PostMapping("/deleteByBatch/{ids}")
    public Result deleteBatch(@PathVariable("ids") List<Long> ids) {
    userService.removeByIds(ids);
        return Result.success("删除成功");
    }

    @ApiOperation("基本查询")
    @GetMapping("/findAll")
    public Result findAll() {
        return Result.success(userService.list());
    }

    @ApiOperation("根据id查询")
    @GetMapping("/findOne")
    public Result findOne(Integer id) {
        return Result.success(userService.getById(id));
    }


    @ApiOperation(value = "分页+条件查询用户", notes = "用户中要含角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：启用|禁用", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isVip", value = "是否为会员: 0|1", dataType = "int", paramType = "query")
    })

    @GetMapping("/query")
    public Result findPage(Integer pageNum, Integer pageSize, String username, String status, Integer roleId,Integer isVip) {

        IPage<UserExt> p =userService.query(pageNum,pageSize,username, status,roleId,isVip);
       return Result.success( p);

    }

    @ApiOperation(value = "根据id查找用户", notes = "id必须存在且有效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "String")
    })
    @GetMapping("queryById/{id}")
    public Result queryById (@PathVariable Long id){
      return Result.success( userDao.selectById(id));
    }

    @ApiOperation(value = "设置用户为Vip", notes = "id存在且有效")
    @PutMapping("/setVip/{id}")
    public Result setVip(@PathVariable Long id) {

        User user = userDao.selectById(id);
        if (user.getIsVip() == 1){
            return Result.failure(ResultCode.DATA_EXISTED);
        }
        user.setId(id);
        user.setIsVip(1);
        user.setExpiresTime(LocalDateTime.now().plusMonths(1));
        userDao.updateById(user);

        return Result.success("更新成功");
    }

    @ApiOperation(value = "更新用户信息", notes = "id必须存在且有效，如果username存在则必须唯一")
    @PutMapping("/update")
    public Result update(@RequestBody User user) {

        userDao.updateById(user);

        return Result.success("更新成功");
    }

}

